package com.zhhtech.zhh

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Process
import androidx.multidex.MultiDex
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.vasdolly.helper.ChannelReaderUtil
import com.umeng.commonsdk.UMConfigure
import com.umeng.socialize.PlatformConfig
import com.zhhtech.zhh.api.core.ApiHelper
import com.zhhtech.zhh.api.core.service.IRouterService
import com.zhhtech.zhh.api.core.service.RouterServiceBootstrap
import com.zhhtech.zhh.common.UserCache
import com.zhhtech.zhh.common.WX_APP_ID
import com.zhhtech.zhh.ui.AppCore
import com.zhhtech.zhh.ui.activity.commom.LoginActivity
import com.zhhtech.zhh.ui.activity.main.MainActivity
import com.zhhtech.zhh.ui.ext.startActivityExt
import com.zhhtech.zhh.ui.utils.Logger
import com.zhhtech.zhh.ui.utils.SPUtils
import me.jessyan.autosize.AutoSize

class App : Application() {
    companion object {
        lateinit var app: com.zhhtech.zhh.App
        lateinit var channel: String
        fun init() {
            com.zhhtech.zhh.App.Companion.app.init()
        }
    }

    override fun onCreate() {
        super.onCreate()
        val processName = getProcessName(this)
        if (packageName == processName) {
            com.zhhtech.zhh.App.Companion.app = this
            AppCore.init(this)
            if (SPUtils.getBoolean("sp_key_secret", false)) {
                init()
            }
        }
    }

    private fun init() {
        AutoSize.checkAndInit(this)
//        val debug = BuildConfig.DEBUG
        val debug = true
        ApiHelper.initHttp(debug)
        Logger.isDebug = debug
        CrashReport.initCrashReport(applicationContext, "5c6a4f9a0e", BuildConfig.DEBUG)
        com.zhhtech.zhh.App.Companion.channel = ChannelReaderUtil.getChannel(applicationContext) ?: BuildConfig.channel
        UMConfigure.preInit(this, "64083ed7d64e686139452e51", com.zhhtech.zhh.App.Companion.channel)
        UMConfigure.init(this, "64083ed7d64e686139452e51", com.zhhtech.zhh.App.Companion.channel, UMConfigure.DEVICE_TYPE_PHONE, "")
        // 微信设置
        PlatformConfig.setWeixin(WX_APP_ID, "3b1ced1d792c368ed9e21a9ea5b6f369")
        PlatformConfig.setWXFileProvider("${BuildConfig.APPLICATION_ID}.fileprovider")
        RouterServiceBootstrap.register(object : IRouterService {
            override fun routerLogin(context: Context) = context.startActivityExt<LoginActivity>()
            override fun routerMain(context: Context) = context.startActivityExt<MainActivity>()
            override fun isLogin() = UserCache.isLogin
            override fun logout() = UserCache.logout()

            override suspend fun autoLogin(): Boolean {
//                val phone = SPUtils.getString("key_phone")
//                val password = SPUtils.getString("key_password")
//                if (phone.isNotEmpty() && password.isNotEmpty()) {
//                    API_SERVICE.loginAndUserInfo(phone, password)
//                    return true
//                }
//                return false
                return true
            }
        })
    }

    override fun attachBaseContext(context: Context?) {
        super.attachBaseContext(context)
        MultiDex.install(this)
//        com.zhhtech.zhh.ui.helper.DemoHelper.loadLibrary()
    }

    private fun getProcessName(context: Context): String? {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val runningApps = am.runningAppProcesses ?: return null
        for (proInfo in runningApps) {
            if (proInfo.pid == Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName
                }
            }
        }
        return null
    }
}